<template>
  <div class="container">
    <div class="top">
      <el-input
        style="width:200px;margin-bottom:5px"
        v-model="params.orgid"
        placeholder="请输入店铺ID"
        clearable
      ></el-input>
      <el-input
        style="width:200px;margin-bottom:5px"
        v-model="params.code"
        placeholder="请输入订单编码"
        clearable
      ></el-input>
      <el-date-picker type="date" v-model="params.date" placeholder="请选择订单日期"></el-date-picker>
      <el-select v-model="params.status" placeholder="请选择订单状态">
        <el-option v-for="(item) in options" :key="item.id" :label="item.name" :value="item.id"></el-option>
      </el-select>

      <el-button
        size="small"
        type="primary"
        class="handleSearch"
        icon="el-icon-search"
        @click="handleSearch()"
      >查询</el-button>
      <el-button size="small" type="primary" class="handleSearch" @click="getErpEnquiry()">拉取ERP要货单</el-button>
    </div>

    <el-table max-height="600":data="tableData" border style="width: 100%" empty-text="暂无数据">
      <el-table-column
        :prop="item.align"
        :label="item.label"
        :align="item.align"
        :width="item.width"
        v-for="(item, index) in column"
        :key="index"
      >
        <template slot-scope="scope">
          <div v-if="item.prop == 'cz'">
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-info"
              @click="particulars(scope)"
            >查看所属订单</el-button>
          </div>
          <div v-if="item.prop == 'status'">
            {{
            scope.row[item.prop] == 0 && '待提交' ||
            scope.row[item.prop] == 1 && '待发货' ||
            scope.row[item.prop] == 2 && '已发货' ||
            scope.row[item.prop] == 3 && '已收货' || ''
            }}
          </div>
          <div v-else>{{scope.row[item.prop]}}</div>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination @page="handleSizeChange" @pagesize="handleCurrentChange" :pageData="pageData"></pagination>

    <!-- 详情Dailog -->
    <shopOrderDialog :option="shopOrderDialog"></shopOrderDialog>
  </div>
</template>

<script>
import { shopOrderGoods, getErpEnquiry } from "@/api/enquiry/enquiryList";
import { parseTime } from "@/utils/index";
import Pagination from "@/components/pagination";
import ShopOrderDialog from "@/components/enquiry/shopOrderDialog/goods";

export default {
  components: { Pagination, ShopOrderDialog },
  data() {
    return {
      shopOrderDialog: {
        show: false,
        data: {}
      },
      //页面数据
      tableData: [],
      //分页
      pageData: {
        pageSizes: [10, 20],
        pageSize: 10,
        page: 1,
        total: null,
        layout: "total, sizes, prev, pager, next, jumper"
      },
      params: {
        orgid: "",
        code: "",
        date: new Date(),
        status: -1
      },
      options: [
        { id: -1, name: "全部" },
        { id: 0, name: "待提交" },
        { id: 1, name: "待发货" },
        { id: 2, name: "已发货" },
        { id: 3, name: "已收货" }
      ],
      column: [
        { label: "商品code", prop: "productcode", width: "", align: "center" },
        { label: "商品名称", prop: "productname", width: "", align: "center" },
        { label: "总要货数量", prop: "qty", width: "", align: "center" },
        { label: "实际配货数量", prop: "pick_qty", width: "", align: "center" },
        { label: "当前库存数量", prop: "stock", width: "", align: "center" },
        { label: "操作", prop: "cz", width: "", align: "center" }
      ]
    };
  },

  created() {
    this.getData();
  },
  methods: {
    //页面数据
    getData() {
      const data = {
        orgid: this.params.orgid || "-1",
        code: this.params.code,
        date: parseTime(this.params.date).split(" ")[0],
        status: this.params.status,
        limit: this.pageData.pageSize,
        page: this.pageData.page
      };
      shopOrderGoods(data)
        .then(res => {
          if (res.code == 0) {
            this.tableData = res.data.data;
            this.pageData.page = res.data.current_page;
            this.pageData.pagesize = res.data.per_page;
            this.pageData.total = res.data.total;
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    // 搜索
    handleSearch() {
      this.pageData.page = 1;
      this.pageData.total = null;
      if (this.params.date == null) {
        this.$message({
          type: "info",
          message: "请选择时间"
        });
      } else {
        this.getData();
      }
      console.log(this.params);
    },
    // erp搜索
    getErpEnquiry() {
      const data = {
        orgid: this.params.orgid || "-1",
        date: parseTime(this.params.date).split(" ")[0]
      };
      getErpEnquiry(data)
        .then(res => {
          if (res.code == 0) {
            this.$message({
              type: "success",
              message: res.msg
            });
            this.handleSearch();
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    //选取页数
    handleSizeChange(page) {
      this.pageData.page = page;
      this.getData();
    },
    //每页多少条
    handleCurrentChange(val) {
      this.pageData.page = 1;
      this.pageData.pageSize = val;
      this.getData();
    },
    //详情
    particulars(scope) {
      this.shopOrderDialog.show = true;
      this.shopOrderDialog.data = {
        orgid: this.params.orgid || "-1",
        code: this.params.code,
        date: parseTime(this.params.date).split(" ")[0],
        status: this.params.status,
        productcode: scope.row.productcode
      };
    }
  }
};
</script>
<style scoped>
.top {
  position: relative;
}

.add {
  position: absolute;
  right: 0;
  top: 4px;
}
.pagination {
  text-align: right;
  padding: 10px;
}
</style>